local GoodsData = class("GoodsData")

function GoodsData:ctor(id,count) 
    self.cfg=configData.cfg_items[id]
    assert(self.cfg,"error create GoodsData")
	self.id= self.cfg.id or 0         
	self.name=self.cfg.name or ""
	self.price=self.cfg.price or 0
    self.count= count and count or -1 -- -1代表无限
    self.moneytype = self.cfg.moneytype
    self.overlying = self.cfg.overlying --堆叠数量
    self.itemtype = self.cfg.itemtype --道具类型
    self:setPopTime(self.cfg.poptime)
    --self.isNew=false --新加入仓库
end

function GoodsData:setData(id,name,price,count)
    self.id= id          
	self.name=name
	self.price=price
    self.count=count
    --self.isNew=isnew
end

function GoodsData:setCount(count)
    self.count=count
end

function GoodsData:getCount(count)
    return self.count
end

function GoodsData:getCapacity()
    return configData.cfg_items[self.id].overlying
end

function GoodsData:getQuality()
    return configData.cfg_items[self.id].quality 
end

--	NPC最终卖出价=NPC默认卖出价×（1+流行滞销修正） ； 其中，流行修正系数=1.5，滞销修正系数=0.5
function GoodsData:getBuyPrice(citynow,num)
    if citynow == nil then
        citynow=RoleData.player_info.stageid
    end
    if num == nil then
        num = 1
    end
    local price = configData.cfg_items[self.id].price
    local hotmodulus = 0
    if self:getQuality() == 1 then       
        if self:isHot() then
            hotmodulus = 1.5
        elseif self:isUnSalable() then
            hotmodulus = 0.5
        end
        return formulas.getGoods1BuyPrice(price,hotmodulus)*num
    elseif self:getQuality() == 2 then
        local cityorigin = configData.cfg_items[self.id].origin
        local distance = Utils.calCityDistance(citynow,cityorigin)     
        local defPrice = formulas.getGoods2DefaultPrice(price,distance)
        local defStore = formulas.getGoods2DefaultStore(DyTradeStore:getGoods2ConstA(self.id),RoleData.lastDayLogins) 
        local curStore = DyTradeStore:getGoods2Store(self.id)
        return formulas.getGoods2BuyPrice(defPrice,defStore,curStore,num)
    end

end
--NPC最终收购价= NPC默认卖出价×（1+本城镇与该商品独售城市的距离/100）×（1+流行滞销修正） 其中，【如果距离=0，则距离=-50】
function GoodsData:getSellPrice(citynow,num)
    if citynow == nil then
        citynow=RoleData.player_info.stageid
    end
    if num == nil then
        num = 1
    end
    local price = configData.cfg_items[self.id].price
    local cityorigin = configData.cfg_items[self.id].origin
    local distance = Utils.calCityDistance(citynow,cityorigin)
    local hotmodulus = 0
    if self:getQuality() == 1 then       
        if self:isHot() then
            hotmodulus = 1.5
        elseif self:isUnSalable() then
            hotmodulus = 0.5
        end
        return formulas.getGoods1SellPrice(price,hotmodulus,distance)*num
    elseif self:getQuality() == 2 then     
        local defPrice = formulas.getGoods2DefaultPrice(price,distance)
        local defStore = formulas.getGoods2DefaultStore(DyTradeStore:getGoods2ConstA(self.id),RoleData.lastDayLogins) 
        local curStore = DyTradeStore:getGoods2Store(self.id)
        return formulas.getGoods2SellPrice(defPrice,defStore,curStore,num)
    end
end

function GoodsData:setPopTime(str)
    self.trend = ""

    local tab = string.split(str,"&")
    local hots = string.split(tab[1],"#")
    local unsalables = string.split(tab[2],"#")

    local date = ServerTime.getDate()
    local tab_hotbegin = {year=date.year, month=date.month, day=date.day, hour=tonumber(hots[1])/100,min=tonumber(hots[1])%100,sec=0,isdst=date.isdst}
    local tab_hotend = {year=date.year, month=date.month, day=date.day, hour=tonumber(hots[2])/100,min=tonumber(hots[2])%100,sec=0,isdst=date.isdst}
    local time_hotbegin = os.time(tab_hotbegin)
    local time_hotend = os.time(tab_hotend)
    if ServerTime.getTime()>=time_hotbegin and ServerTime.getTime()<time_hotend then
        self.trend = "hot"
        return
    end
    local tab_unsalablesbegin = {year=date.year, month=date.month, day=date.day, hour=tonumber(unsalables[1])/100,min=tonumber(unsalables[1])%100,sec=0,isdst=date.isdst}
    local tab_unsalablesend = {year=date.year, month=date.month, day=date.day, hour=tonumber(unsalables[2])/100,min=tonumber(unsalables[2])%100,sec=0,isdst=date.isdst}
    local time_unsalablesbegin = os.time(tab_unsalablesbegin)
    local time_unsalablesend = os.time(tab_unsalablesend)
    if ServerTime.getTime()>=time_unsalablesbegin and ServerTime.getTime()<time_unsalablesend then
        self.trend = "unsalable"
        return
    end
end
--流行
function GoodsData:isHot()
    return self.trend == "hot"
end
--滞销
function GoodsData:isUnSalable()
    return self.trend == "unsalable"
end

function GoodsData:getTax()
    return DyTradeStore:getTax(self:getQuality())
end

function GoodsData:setNew(isnew)
    self.isNew = isnew
end

function GoodsData:clear()
    self.id= 0          
	self.name=""
	self.price=0
    self.count=0
    self.isNew=false --新加入仓库
end

return GoodsData